﻿using System;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Localisation;
using Rio.Framework.Pages;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Controls;
using Rio.Framework.Pages.Events;
using Rio.Framework.Pages.ModalControls;
using Rio.Framework.Pages.Viewer;
using System.Windows.Threading;
using Rio.Framework.WCFService;
using System.IO;
using Rio.Framework.Pages.Manager;


namespace Rio.Framework.Pages.ChildWindows
{
    public partial class AddDocumentChildWindow
    {
        #region Enums

        enum SelectModeDocument
        {
            Thumbnail,
            Viewer,
            List
        }

        #endregion

        #region Attributs

        protected DispatcherTimer m_dtRioscanTimer;
        protected string m_sRefreshScanGuid = String.Empty;
        protected Rio.Framework.WCFService.RioWcfServices.WCFServiceClient m_oWCFServiceClient;

        private SelectModeDocument DisplayMode;
        public IndexPanel m_oControlImportPanel { get; set; }
        private SplashScreen m_oSplashScreen = new SplashScreen();
        private Organization m_oOrganization;
        private FolderType m_oFolderType;
        private bool _isBusy;
        private bool selectAllImageAfterScan;
        #endregion

        #region Constructors

        public AddDocumentChildWindow()
        {
            InitializeComponent();

            m_oWCFServiceClient = ServiceManager.GetRioWCFServiceClient();
            m_oWCFServiceClient.RefreshFileExistsCompleted += L_oWCFServiceClient_RefreshFileExistsCompleted;
            m_oWCFServiceClient.CreateRefreshFileCompleted += L_oWCFServiceClient_CreateRefreshFileCompleted;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AddDocumentChildWindow" /> class.
        /// </summary>
        /// <param name="p_oFolderType">Type of the folder.</param>
        /// <param name="p_oOrganization">The organization.</param>
        /// <param name="p_oFolder">The folder.</param>
        /// <param name="p_iMaxNumberOfFilesImported">The p_i max number of files imported.</param>
        public AddDocumentChildWindow(FolderType p_oFolderType, Organization p_oOrganization, Folder p_oFolder, int p_iMaxNumberOfFilesImported)
            : this()
        {

            this.Header = Resx.GetLabel(ResxFolder.ADD_DOCUMENT);
            this.KeyUp += MessageBoxChildWindow_KeyUp;
            m_oOrganization = p_oOrganization;
            m_oFolderType = p_oFolderType;

            m_oControlImportPanel = new IndexPanel(ListVignette, ViewerControl, ListViewer, m_oOrganization, m_oFolderType, p_oFolder);

            if (p_oFolder != null)
            {
                m_oControlImportPanel.DisablePanelForSearchMode();
            }

            //Disable buttonToSearch to avoid change Organization/FolderType/Indexes
            m_oControlImportPanel.UseChronoGeneration = false;
            m_oControlImportPanel.Indexed += m_oControlImportPanel_Indexed;
            m_oControlImportPanel.BeforeIndexing += m_oControlImportPanel_BeforeIndexing;

            ControlImportPanelParent.Child = m_oControlImportPanel;
            InitLabel();

            //**ListViewer events
            ListViewer.CurrentDisplayMode = AppCode.Enums.AppDisplayMode.AddDocument;
            ListViewer.ActivateThumbnailMode += ActivateThumbnailMode;
            ListViewer.ActivateViewerMode += ActivateViewerMode;
            ListViewer.ItemsSelectedToDisplay += ItemsSelectedToDisplay;
            ListViewer.DirectorySelectedToDisplay += ListViewer_DirectorySelectedToDisplay;
            ListViewer.DirectorySelected += ListVignette_DirectorySelected;
            ListViewer.ItemsSelectedFileName += ItemsSelectedFileName;
            ListViewer.ImportRequest += ImportRequest;
            ListViewer.ScanRequest += ScanRequest;

            //**ViewerControl events
            ViewerControl.CurrentDisplayMode = AppCode.Enums.AppDisplayMode.AddDocument;
            ViewerControl.ActivateListMode += ActivateListMode;
            ViewerControl.ActivateThumbnailMode += ActivateThumbnailMode;
            ViewerControl.DirectorySelectedToDisplay += ViewerControl_DirectorySelectedToDisplay;
            ViewerControl.DirectorySelected += ListVignette_DirectorySelected;
            ViewerControl.ImportRequest += ImportRequest;
            ViewerControl.ScanRequest += ScanRequest;

            //**ViewerControl events
            ListVignette.CurrentDisplayMode = AppCode.Enums.AppDisplayMode.AddDocument;
            ListVignette.FileInPostOrder = true;
            ListVignette.EndOfImport += ViewerControl_EndOfImport;
            ListVignette.ActivateViewerMode += ActivateViewerMode;
            ListVignette.ActivateListMode += ActivateListMode;
            ListVignette.ItemsSelectedToDisplay += ItemsSelectedToDisplay;
            ListVignette.ItemsSelectedFileName += ItemsSelectedFileName;
            ListVignette.RefreshThumbnails += ListVignette_RefreshThumbnails;
            ListVignette.DirectorySelectedToDisplay += ListVignette_DirectorySelectedToDisplay;
            ListVignette.DirectorySelected += ListVignette_DirectorySelected;
            ListVignette.MaxNumberOfFilesImported = p_iMaxNumberOfFilesImported;
            ListVignette.ImportedDocumentRefreshed += ListVignette_ImportedDocumentRefreshed;
            ListVignette.ImportRequest += ImportRequest;
            ListVignette.ScanRequest += ScanRequest;


            //Set thumbnail by default
            ViewerControl.Visibility = Visibility.Collapsed;
            ListViewer.Visibility = Visibility.Collapsed;
            ListVignette.Visibility = Visibility.Visible;
            DisplayMode = SelectModeDocument.Thumbnail;


            //SettingsDomainContext l_sdcSettingsDomainContext = new SettingsDomainContext();
            //l_sdcSettingsDomainContext.GetDefaultDisplayMode(delegate(InvokeOperation<bool> ret)
            //{
            //    if (ret.Value)
            //    {
            //        ViewerControl.Visibility = System.Windows.Visibility.Collapsed;
            //        ListVignette.Visibility = System.Windows.Visibility.Visible;
            //        DisplayMode = SelectModeDocument.Thumbnail;
            //    }
            //    else
            //    {
            //        ViewerControl.Visibility = System.Windows.Visibility.Visible;
            //        ListVignette.Visibility = System.Windows.Visibility.Collapsed;
            //        DisplayMode = SelectModeDocument.Viewer;
            //    }
            //}, null);
        }

        #endregion

        #region Events implementation


        /// <summary>
        /// Event raised when clicking on a thumbnail image
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DocumentSelectedEventArgs" /> instance containing the event data.</param>
        private void ItemsSelectedToDisplay(object sender, DocumentSelectedEventArgs args)
        {
            ViewerControl.IndexPage = 1;
            ViewerControl.m_oItemsSource = args.ListAllRioImages;
            ViewerControl.CurrentSelectedDirectory = (sender as UCViewerCore).CurrentSelectedDirectory;
            ViewerControl.SelectedDocumentIndex = args.SelectedDocumentIndex;
            ViewerControl.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, ListVignette.ListAllDocFileName, args.SelectedDocumentIndex, false);
            ViewerControl.Visibility = System.Windows.Visibility.Visible;
            ListVignette.Visibility = System.Windows.Visibility.Collapsed;
            ListViewer.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// Event switching from a mode to Viewer mode
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ActivateViewerMode(object sender, EventArgs e)
        {
            ListViewer.Visibility = System.Windows.Visibility.Collapsed;
            ViewerControl.Visibility = System.Windows.Visibility.Visible;
            ListVignette.Visibility = System.Windows.Visibility.Collapsed;

            ViewerControl.UserId = ListVignette.UserId;
            ViewerControl.ListAllDocFileName = ListVignette.ListAllDocFileName;
            ViewerControl.m_oItemsSource = ListVignette.m_oItemsSource;
            ViewerControl.CurrentSelectedDirectory = ListVignette.CurrentSelectedDirectory;

            switch (DisplayMode)
            {
                case SelectModeDocument.Thumbnail:
                    if (((System.Windows.Controls.ListBox)ListVignette.FindName("ListBoxVignette")).SelectedIndex != -1)
                    {
                        ViewerControl.IndexPage = 1;
                        ViewerControl.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, ListVignette.ListAllDocFileName, ((System.Windows.Controls.ListBox)ListVignette.FindName("ListBoxVignette")).SelectedIndex, false);
                    }
                    else
                    {
                        ViewerControl.IndexPage = 1;
                        ViewerControl.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, ListVignette.ListAllDocFileName, 0, false);
                    }
                    break;

                case SelectModeDocument.List:
                    if (ListViewer.SelectedImageIndex > -1)
                        ViewerControl.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, ListViewer.ListAllDocFileName, ListViewer.SelectedImageIndex, false);
                    else
                        ViewerControl.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId, ListViewer.ListAllDocFileName, 0, false);
                    break;
            }

            DisplayMode = SelectModeDocument.Viewer;
        }

        /// <summary>
        /// Event switching from a mode to Thumbnail mode
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ActivateThumbnailMode(object sender, EventArgs e)
        {
            ListViewer.Visibility = System.Windows.Visibility.Collapsed;

            ViewerControl.Visibility = System.Windows.Visibility.Collapsed;
            ViewerControl.ClearViewer();

            ListVignette.Visibility = System.Windows.Visibility.Visible;

            switch (DisplayMode)
            {
                case SelectModeDocument.List:
                    if (ListViewer.SelectedImages != null)
                    {
                        ListVignette.SelectedImages = ListViewer.SelectedImages;
                    }
                    ListViewer.SetActuallyRankingList();
                    ListVignette.RankedListFromViewer = ListViewer.RankedListFromViewer;
                    break;
                case SelectModeDocument.Viewer:
                    ListVignette.SelectedImages = ViewerControl.GetSelectedImage<Rio.Framework.WCFService.RioWcfServices.ImageRio>();
                    ListVignette.RankedListFromViewer = ViewerControl.ListAllDocFileName;
                    ListVignette.LoadVignettes(ViewerControl.SelectedDocumentIndex);
                    break;
            }

            m_oControlImportPanel.m_oListSelectedFilename = ListVignette.GetSelectedImage<Rio.Framework.WCFService.RioWcfServices.ImageRio>();
            DisplayMode = SelectModeDocument.Thumbnail;
        }

        /// <summary>
        /// Event switching from a mode to List mode
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ActivateListMode(object sender, EventArgs e)
        {
            ViewerControl.Visibility = System.Windows.Visibility.Collapsed;
            ViewerControl.ClearViewer();

            ListVignette.Visibility = System.Windows.Visibility.Collapsed;

            ListViewer.ListAllDocFileName = ListVignette.ListAllDocFileName;
            ListViewer.m_oItemsSource = ListVignette.m_oItemsSource;
            ListViewer.UserId = ListVignette.UserId;
            ListViewer.Images = ListVignette.m_oItemsSource;
            ListViewer.CurrentSelectedDirectory = (sender as UCViewerCore).CurrentSelectedDirectory;
            ListViewer.ManageToolbarState();
            ListViewer.Visibility = System.Windows.Visibility.Visible;

            switch (DisplayMode)
            {
                case SelectModeDocument.Thumbnail:
                    if (ListVignette.SelectedImages != null && ListVignette.SelectedImages.Count > 0)
                    {
                        ListViewer.SelectedImages = ListVignette.SelectedImages;
                    }
                    break;
                case SelectModeDocument.Viewer:
                    ListViewer.SelectedImages = ViewerControl.GetSelectedImage<Rio.Framework.WCFService.RioWcfServices.ImageRio>();
                    break;
            }

            DisplayMode = SelectModeDocument.List;
        }

        /// <summary>
        /// Itemses the name of the selected file.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DocumentSelectedEventArgs" /> instance containing the event data.</param>
        private void ItemsSelectedFileName(object sender, DocumentSelectedEventArgs args)
        {
            if (args.ListAllFileName != null && args.ListAllFileName.Count > 0)
            {
                m_oControlImportPanel.m_oListSelectedFilename = args.ListAllSelectedFiles;
            }
            else
            {
                m_oControlImportPanel.m_oListSelectedFilename.Clear();
            }
        }

        /// <summary>
        /// Handles the ImportRequest event of the ListViewer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ImportRequest(object sender, EventArgs e)
        {
            IEnumerable<FileInfo> filesToImport = null;
            if (ListVignette.SelectFilesToImport(ref filesToImport))
            {
                _isBusy = true;

                if (ListVignette.Visibility == System.Windows.Visibility.Collapsed)
                {
                    ActiveVignetteModeForImportation(filesToImport);
                }
                else
                {
                    ListVignette.ImportFiles(filesToImport);
                }
            }
        }

        /// <summary>
        /// Handles the ScanRequest event of the ListViewer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ScanRequest(object sender, EventArgs e)
        {
            if (ListVignette.Visibility.Equals(System.Windows.Visibility.Collapsed))
            {
                ActiveVignetteModeForImportation();
                ListVignette.RefreshThumbnails += ListVignette_RefreshThumbnailsForScan;
            }
            else
            {
                SplashScreenManager.Wait(this);
            }
            ListVignette.SetActuallyRankingList();

            m_sRefreshScanGuid = GlobalWebContext.CurrentUser.RefreshScanGuid;
            m_oWCFServiceClient.CreateRefreshFileAsync(m_sRefreshScanGuid);

            Redirection.RioScan("A");
            m_dtRioscanTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 2000) };
            m_dtRioscanTimer.Tick += m_dtRioscanTimer_Tick;
        }

        #region ListViewer

        /// <summary>
        /// Event raised when double click a directory in the listviewer
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs" /> instance containing the event data.</param>
        private void ListViewer_DirectorySelectedToDisplay(object sender, DirectorySelectedEventArgs args)
        {
            ListViewer.SelectedDocumentIndex = 0;
            ListViewer.CurrentSelectedDirectory = args.DirectoryName;
            ListViewer.CurrentSelectedImage = args.SelectedDirectory;

            ListVignette.SelectedDocumentIndex = 0;
            ListVignette.CurrentSelectedDirectory = args.DirectoryName;
            ListVignette.CurrentSelectedImage = args.SelectedDirectory;
            ListVignette.LoadVignettes(0, false);

            //**disable button import
            ListViewer.ScanImportIsEnabled = (args.DirectoryMode == DirectorySelectedEventArgs.Mode.OpenDirectory) ? false : true;

            ListViewer.RefreshItems();
            ListViewer.Visibility = System.Windows.Visibility.Visible;
            ViewerControl.Visibility = System.Windows.Visibility.Collapsed;
            ListVignette.Visibility = System.Windows.Visibility.Collapsed;
        }

        #endregion

        #region Thumbnail

        /// <summary>
        /// Event raised when selecting an item is selected in the thumbnail panel
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs" /> instance containing the event data.</param>
        private void ListVignette_DirectorySelected(object sender, DirectorySelectedEventArgs args)
        {
            if (args.SelectedDirectory != null && args.SelectedDirectory.FileList != null && args.SelectedDirectory.FileList.Count > 0)
            {
                m_oControlImportPanel.m_oListSelectedFilename = ListVignette.GetSelectedImage<Rio.Framework.WCFService.RioWcfServices.ImageRio>();
            }
            else
            {
                m_oControlImportPanel.m_oListSelectedFilename.Clear();
            }
        }

        /// <summary>
        /// Lists the vignette_ imported document refreshed.
        /// </summary>
        /// <param name="p_oImageRioCollection">The p_o image rio collection.</param>
        private void ListVignette_ImportedDocumentRefreshed(System.Collections.ObjectModel.ObservableCollection<Framework.WCFService.RioWcfServices.ImageRio> p_oImageRioCollection)
        {
            ListVignette.ThumbnailPanel_ImportedDocumentRefreshed(p_oImageRioCollection);
            ListViewer.Images = ListVignette.m_oItemsSource;

            //Use to reselect images after scan process
            if (selectAllImageAfterScan)
            {
                ListVignette.SelectImages();
                selectAllImageAfterScan = false;
            }
        }

        /// <summary>
        /// Event raised when double clicking on a thumbnail directory
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs" /> instance containing the event data.</param>
        private void ListVignette_DirectorySelectedToDisplay(object sender, DirectorySelectedEventArgs args)
        {
            ListVignette.SelectedDocumentIndex = 0;
            ListVignette.CurrentSelectedDirectory = args.DirectoryName;
            ListVignette.CurrentSelectedImage = args.SelectedDirectory;
            ListVignette.LoadVignettes(0, false);

            //**disable button import
            ListVignette.ScanImportIsEnabled = (args.DirectoryMode == DirectorySelectedEventArgs.Mode.OpenDirectory) ? false : true;

            ListViewer.Visibility = System.Windows.Visibility.Collapsed;
            ViewerControl.Visibility = System.Windows.Visibility.Collapsed;
            ListVignette.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// Event raised when clicking on the thumbnail refresh button
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ListVignette_RefreshThumbnails(object sender, EventArgs e)
        {
            if (DisplayMode == SelectModeDocument.Viewer)
            {
                ViewerControl.m_oItemsSource = ListVignette.m_oItemsSource;
                ViewerControl.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId,
                                                    ListVignette.ListAllDocFileName, 0, false);
            }
            ListVignette.RefreshThumbnails -= ListVignette_RefreshThumbnails;
            if (ListVignette.ListAllDocFileName == null || ListVignette.ListAllDocFileName.Count < 1)
            {
                SplashScreen_Close();
            }

        }

        /// <summary>
        /// Handles the RefreshThumbnailsForScan event of the m_oListVignetteFromImportPage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ListVignette_RefreshThumbnailsForScan(object sender, EventArgs e)
        {
            ListVignette.RefreshThumbnails -= ListVignette_RefreshThumbnailsForScan;
        }

        #endregion

        #region Viewer

        /// <summary>
        /// Event raised when import is finalized
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ViewerControl_EndOfImport(object sender, EventArgs e)
        {
            _isBusy = false;

            if (DisplayMode == SelectModeDocument.Viewer)
            {
                ViewerControl.LoadImageFromDocument(GlobalWebContext.CurrentUser.UserId,
                                                    ListVignette.ListAllDocFileName, 0, false);
            }
        }

        /// <summary>
        /// Event raised when selecting an item in the viewer
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void ViewerControl_DisplayCompleted(object sender, EventArgs e)
        {
            SplashScreen_Close();
        }

        /// <summary>
        /// Event raised when double click a directory in the viewer
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Rio.Framework.Pages.Events.DirectorySelectedEventArgs" /> instance containing the event data.</param>
        private void ViewerControl_DirectorySelectedToDisplay(object sender, DirectorySelectedEventArgs args)
        {
            ViewerControl.SelectedDocumentIndex = 0;
            ViewerControl.CurrentSelectedDirectory = args.DirectoryName;
            ViewerControl.CurrentSelectedImage = args.SelectedDirectory;

            ListVignette.SelectedDocumentIndex = 0;
            ListVignette.CurrentSelectedDirectory = args.DirectoryName;
            ListVignette.CurrentSelectedImage = args.SelectedDirectory;
            ListVignette.LoadVignettes(0, false);

            //**disable button import
            ViewerControl.ScanImportIsEnabled = args.DirectoryMode != DirectorySelectedEventArgs.Mode.OpenDirectory;

            ViewerControl.RefreshItems();
            ListViewer.Visibility = System.Windows.Visibility.Collapsed;
            ViewerControl.Visibility = System.Windows.Visibility.Visible;
            ListVignette.Visibility = System.Windows.Visibility.Collapsed;
        }


        #endregion

        #endregion

        #region Private functions

        /// <summary>
        /// Handles the KeyUp event of the MessageBoxChildWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs" /> instance containing the event data.</param>
        private void MessageBoxChildWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape && !_isBusy)
            {
                this.DialogResult = false;
                this.Close();
            }
        }

        /// <summary>
        /// Translation
        /// </summary>
        private void InitLabel()
        {
            m_oSplashScreen.Message = Resx.GetLabel(ResxCommon.SPLASHSCREEN_MESSAGE);
        }

        /// <summary>
        /// Splashes the screen_ close.
        /// </summary>
        private void SplashScreen_Close()
        {
            ModalDialogController.Close();
            m_oSplashScreen.Close();
        }

        /// <summary>
        /// Handles the Indexed event of the m_oControlImportPanel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void m_oControlImportPanel_Indexed(object sender, EventArgs e)
        {
            _isBusy = false;
            DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// Handles the BeforeIndexing event of the m_oControlImportPanel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void m_oControlImportPanel_BeforeIndexing(object sender, EventArgs e)
        {
            ListViewer.SetActuallyRankingList();
            ListVignette.SetActuallyRankingList();

            _isBusy = true;
        }

        /// <summary>
        /// Actives the vignette mode.
        /// </summary>
        /// <param name="filesToImport">The files to import.</param>
        private void ActiveVignetteModeForImportation(IEnumerable<FileInfo> filesToImport = null)
        {
            ViewerControl.Visibility = System.Windows.Visibility.Collapsed;
            ListViewer.Visibility = System.Windows.Visibility.Collapsed;
            ViewerControl.ClearViewer();

            ListVignette.Visibility = System.Windows.Visibility.Visible;

            if (filesToImport != null)
            {
                ListVignette.LoadVignetteAndImport(filesToImport);
            }
            else
            {
                ListVignette.LoadVignettes();
            }

            m_oControlImportPanel.m_oListSelectedFilename = ListVignette.GetSelectedImage<Rio.Framework.WCFService.RioWcfServices.ImageRio>();
        }

        /// <summary>
        /// Handles the CreateRefreshFileCompleted event of the L_oWCFServiceClient control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rio.Framework.WCFService.RioWcfServices.CreateRefreshFileCompletedEventArgs" /> instance containing the event data.</param>
        private void L_oWCFServiceClient_CreateRefreshFileCompleted(object sender, Rio.Framework.WCFService.RioWcfServices.CreateRefreshFileCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Error On CreateRefreshFile() -> " + e.Error.Message, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                L_oMessageGetDocument.Show();
                SplashScreenManager.Stop(this);
                this.Cursor = Cursors.Arrow;
            }
            else
            {
                Rio.Framework.WCFService.RioWcfServices.ReturnBoolean l_oReturnBoolean = e.Result;
                if (l_oReturnBoolean.IsValid == false)
                {
                    MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Response not valid On CreateRefreshFile() -> " + l_oReturnBoolean.ErrorMessage, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                    L_oMessageGetDocument.Show();
                    SplashScreenManager.Stop(this);
                    this.Cursor = Cursors.Arrow;
                }
                else
                {
                    if (Convert.ToBoolean(l_oReturnBoolean.Value))
                    {
                        m_dtRioscanTimer.Start();
                    }
                    else
                    {
                        SplashScreenManager.Stop(this);
                        this.Cursor = Cursors.Arrow;
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_dtRioscanTimer_Tick(object sender, EventArgs e)
        {
            m_oWCFServiceClient.RefreshFileExistsAsync(m_sRefreshScanGuid, ServiceManager.GetLicenseKey());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void L_oWCFServiceClient_RefreshFileExistsCompleted(object sender, Rio.Framework.WCFService.RioWcfServices.RefreshFileExistsCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Error On RefreshFileExist() -> " + e.Error.Message, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                L_oMessageGetDocument.Show();
                SplashScreenManager.Stop(this);
                this.Cursor = Cursors.Arrow;
                m_dtRioscanTimer.Stop();
            }
            else
            {
                Rio.Framework.WCFService.RioWcfServices.ReturnBoolean l_oReturnBoolean = e.Result;
                if (l_oReturnBoolean.IsValid == false)
                {
                    MessageBoxChildWindow L_oMessageGetDocument = new MessageBoxChildWindow("", "Response not valid On RefreshFileExist() -> " + l_oReturnBoolean.ErrorMessage, MessageBoxButtons.Ok, MessageBoxIcon.Error);
                    L_oMessageGetDocument.Show();
                    SplashScreenManager.Stop(this);
                    this.Cursor = Cursors.Arrow;
                    m_dtRioscanTimer.Stop();
                }
                else
                {
                    if (!Convert.ToBoolean(l_oReturnBoolean.Value))
                    {
                        selectAllImageAfterScan = true;
                        ListVignette.LoadVignettes(0);
                        SplashScreenManager.Stop(this);
                        this.Cursor = Cursors.Arrow;
                        m_dtRioscanTimer.Stop();
                    }
                }
            }
        }
        #endregion


    }
}

